// pages/allShops/allShops.js
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var type;
var priceType = 1;
var priceTap = 1;
var navBar = 0;
var cateId=0;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    if (navBar == 0) {
      console.log(page)
      wx.request({
        url: url.URl + 'goods',
        data: { page: page, cate_id: cateId},
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading();
          that.setData({
            shops: that.data.shops.concat(res.data.data.data)
          });
          page++;
        }
      });
    } else if (navBar == 1) {
      wx.request({
        url: url.URl + 'goods',
        data: { page: page, new: 1, cate_id: cateId},
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          that.setData({
            shops: that.data.shops.concat(res.data.data.data)
          });
          page++;
        }
      });
    } else if (navBar == 2) {
      wx.request({
        url: url.URl + 'goods',
        data: { page: page, goods_salenum: 1, cate_id: cateId},
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          that.setData({
            shops: that.data.shops.concat(res.data.data.data)
          });
          page++;
        }
      });
    } else if (navBar == 3) {
      console.log(priceType)
      wx.request({
        url: url.URl + 'goods',
        data: { page: page, price: priceType, cate_id: cateId},
        method: 'get',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          that.setData({
            shops: that.data.shops.concat(res.data.data.data)
          });
          page++;
        }
      });
    }

  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navTab: ['全部', '最新', '热销', '价格'],
    currentNavtab: 0,
    shops: [],
    listStyle: 1,
    priceType: 2,
    cateFlag: true,
    cateId:0,
    queS:true
  },
  listStyle: function (e) {
    console.log(e)
    var that = this;
    let idx = e.currentTarget.dataset.index;
    if (idx == 1) {
      that.setData({
        listStyle: 2
      })
    } else if (idx == 2) {
      that.setData({
        listStyle: 1
      })
    }
  },
  classfli: function () {
    this.setData({
      cateFlag: false
    })
  },
  classHidden: function () {
    this.setData({
      cateFlag: true
    })
  },
  getCate() {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'cate',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        if (res.data.code == 200) {
          var cate = res.data.data;
          var NO1 = { name: "全部", id: 0}
          cate.unshift(NO1)
          that.setData({
            cate: cate
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  selShop:function(e){
    var that=this;
    that.setData({
      cateFlag: true,
    })
    // console.log(e)
    var cattId = e.currentTarget.dataset.id
    var currentNavtab = that.data.currentNavtab;
    console.log(cattId)
    console.log(cateId)
    if (cateId == cattId){
       return;
    }else{
      cateId = e.currentTarget.dataset.id;
      page=1;
      that.setData({
        shops:[],
        cateId: cateId
      })
      
      if (currentNavtab==0){
        that.getGoodsList()
      } else if (currentNavtab == 1){
        that.getNewGoods()
      } else if (currentNavtab == 2) {
        that.getsalenumGoods()
      } else if (currentNavtab == 3) {
        that.getPriceGoods()
      }
      
    }
  },
  toSearch:function(){
    wx.navigateTo({
      url: '/pages/searchList/searchList?type=1',
    })
  },
  switchTab: function (e) {
    var that = this;
    let idx = e.currentTarget.dataset.idx;
    let currentNavtab = that.data.currentNavtab;
    if (idx == currentNavtab && idx != 3) {
      return false;
    } else {
      page = 1
      that.setData({
        shops: []
      })
      if (idx == 0) {
        navBar = 0;
        that.getGoodsList();
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      } else if (idx == 1) {
        navBar = 1;
        that.getNewGoods()
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      } else if (idx == 2) {
        navBar = 2;
        that.getsalenumGoods()
        priceType = 1;
        priceTap = 1;
        that.setData({
          priceType: 2
        })
      } else if (idx == 3) {
        navBar = 3;
        console.log(priceType)
        console.log(priceTap)
        if (priceTap == 1) {
          priceType = 1;
          that.getPriceGoods()
          that.setData({
            priceType: 1
          })
          priceTap = 2;
        } else if (priceTap == 2) {
          priceType = 0;
          that.getPriceGoods()
          that.setData({
            priceType: 0
          })
          priceTap = 1;
        }
      }
      that.setData({
        currentNavtab: idx
      })
    }
  },
  getGoodsList(){
    var that=this;
    console.log(page)
    if (page > 1) {
      page--;
    }

    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods',
      data: { page: page, cate_id: cateId},
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading()
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          console.log(res.data.data.data)
          that.setData({
            shops: res.data.data.data
          })
          that.setData({
            queS: true
          })
        } else if(res.data.code==201){
          that.setData({
            queS:false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
        
      }
    })
  },
  getsalenumGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods',
      data: { page: page, goods_salenum: 1, cate_id: cateId},
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;
        wx.hideLoading()
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            shops: res.data.data.data
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        }  else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getNewGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods',
      data: { page: page, new: 1, cate_id: cateId},
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;
        wx.hideLoading()
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            shops: res.data.data.data
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        }  else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getPriceGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods',
      data: { page: page, price: priceType, cate_id: cateId},
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg;
        wx.hideLoading()
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            shops: res.data.data.data
          })
          that.setData({
            queS: true
          })
        } else if (res.data.code == 201) {
          that.setData({
            queS: false
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.getGoodsList()
    that.getCate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    page = 1;
    cateId=0;
  },
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})